/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var txtUsername = document.getElementById("txtUsername");
var txtPassword = document.getElementById("txtPassword");
var btLogin=document.getElementById("btLogin");
var infoDiv=document.getElementById("infoDiv");

var request=null;

btLogin.onclick=login;

function login(e){
    e.preventDefault();

    infoDiv.innerHTML = '<img src="img/loading.gif" width="25px"' +
    'height="25px"/> Đang đăng nhập.';
    if (checkBlank()) {
        request=new XMLHttpRequest();
        var url="login.action";
        var params = "loginUsername="+ txtUsername.value+ "&loginPassword="+txtPassword.value;

        request.open("POST", url,true);
        request.onreadystatechange=loginResult;
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send(params);
    }
}

function loginResult(){
    if (request.readyState==4) {
        if (request.status==200) {
            var result = eval('('+ request.responseText +')');
            if (result.message=='success') {
                window.location="index.action";
            } else if (result.message=='failure') {
                infoDiv.innerHTML= result.info.fontcolor("red");
            }
        }
    }
}
function checkBlank(){
    if (txtUsername.value==''||txtPassword.value=='') {
        infoDiv.innerHTML='Xin vui lòng nhập đầy đủ thông tin.'.fontcolor("red");
        return false;
    }
    return true;
}